package com.example.phonesearch;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.io.*;

public class MainActivity extends AppCompatActivity {
    private EditText etPhone;
    private Button btnQuery;
    private TextView tvResult;
    private CheckBox checkBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //申请权限
        setContentView(R.layout.activity_main);
        etPhone = (EditText) findViewById(R.id.et_phone);
        btnQuery = (Button) findViewById(R.id.btn_query);
        tvResult = (TextView)findViewById(R.id.tv_result);
        checkBox =  (CheckBox)findViewById(R.id.checkbox);
        getDatabaseFromassets();
        //监听手机的状态，有来电，响铃时的状态，查询来电号码的归属地
        listenPhoneState();

        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取输入框输入的号码
                String phone=etPhone.getText().toString().trim();
                //查询phone的归属地
                String address=AddressDBUtils.queryAddress(getApplicationContext(),phone);
                //显示文本框
                tvResult.setText(address);
                Log.i("Main","area ="+address);
            }
        });
        //监听文本输入框，当输入框中的文本发生变化，进行查询
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String phone=etPhone.getText().toString().trim();
                String address=AddressDBUtils.queryAddress(getApplicationContext(),phone);
                tvResult.setText(address);
            }
        });
    }

    private void listenPhoneState() {
        //监听手机的状态，有来电，响铃时的状态，查询来电号码的归属地
        //1-获取电话管理者对象
        //系统服务提供
        TelephonyManager tm= (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        //2-由电话管理者对象tm，监听电话状态
        //参数1-监听器对象
        //参数二-监听的时间代码int,监听电话状态
        tm.listen(new MyPhonestateListener(),PhoneStateListener.LISTEN_CALL_STATE);
    }
    class MyPhonestateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            //电话状态改变，处理
            switch (state){
                case TelephonyManager.CALL_STATE_IDLE:
                    //空闲
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //通话
                    break;
                case  TelephonyManager.CALL_STATE_RINGING:
                    String phone=incomingNumber;//来电号码
                    String address=AddressDBUtils.queryAddress(getApplicationContext(),phone);
                    Toast.makeText(getApplicationContext(),address,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }
    private void getDatabaseFromassets() {
        File file = new File(getFilesDir(),"address.db");
        if (file.exists()){
            Toast.makeText(getApplicationContext(),"文件已存在",Toast.LENGTH_LONG).show();
            return;
        }
        Toast.makeText(getApplicationContext(),"文件不存在，复制文件", Toast.LENGTH_LONG).show();
        //资产目录下的文件不会编译，没有id
        //操作数据库，将资产目录下的数据库文件复制到手机的
        //data/data/程序包名//files/address.db
        //源文件，assets.db
        //封装好一个和asstes目录相关联的流，通过资产目录管理者
        //目的文件data/data/程序包名//files/address.db
        try {
            Toast.makeText(getApplicationContext(),"复制数据库文件",Toast.LENGTH_LONG).show();
            InputStream in=getAssets().open("address.db");
            OutputStream out =new FileOutputStream(new File(getFilesDir(),"address.db"));
            //复制
            byte[]buf=new byte[1024];
            int len=0;
            while((len=in.read(buf))!=-1){
                out.write(buf,0,len);
                out.flush();
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
